import { find, ProgressBar, SpriteFrame, Vec3 } from 'cc';
import { Vec2 } from 'cc';
import { Enum } from 'cc';
import { _decorator, Component, Node } from 'cc';
import { gameComponent } from '../gameComponent';
import { set } from '../../../extensions/PlayableBuild/@types/packages/scene/@types/cce/utils/lodash';
import { Prefab } from 'cc';
import { instantiate } from 'cc';
const { ccclass, property } = _decorator;

enum enemyType {
    Boss,
    Master
}

let rangeRate = 3;
let speedRate = 0.02;

@ccclass('enemyComponent')
export class enemyComponent extends Component {

    @property({type : Enum(enemyType)}) //敌人类型
    curEnemy : enemyType = enemyType.Master;

    @property(Prefab)
    meat_prefab : Prefab = null; // 肉

    @property({type : Number}) //移动速度
    moveSpeed = 250;

    @property({type : Number}) //攻击范围
    attackRange = 50;

    @property({type : Number}) //攻击力
    attackPower = 5;

    @property({type : Number}) //攻击速度
    attackSpeed = 1;

    @property({type : Number}) //血量 如果血量为-1 则视为无限血量
    blood = 100;

    private curBlood = this.blood;

    private curMoveDir = new Vec2(0,1);

    start() {

    }

     //生成掉落的资源
    createMeat(){
        console.log("==========掉落肉==========")
        let meat = instantiate(this.meat_prefab);
        meat.setPosition(this.node.position);
        meat.parent = this.node.parent;
        find("Canvas").getComponent(gameComponent).addContent(meat);
    }

    attackHero(hero){
        if(!this.isCanAttack) return;
        console.log("=========敌人可以攻击英雄========");
        this.attackTimes = 0;
        find("Canvas").getComponent(gameComponent).createEnemyAttackHeroEffect(this.node);
    }

    attackTimes = 0;
    isCanAttack = true;
    updateAttackChiling(){
        this.attackTimes++;
        if(this.attackTimes >= 40){
            this.isCanAttack = true;
        }else{
            this.isCanAttack = false;
        }
    }

    updateEnemyPosition(hero : Node){

        if(this.curBlood <= 0) return;

        let subPos = new Vec2();
        let hero_pos = new Vec2(hero.position.x,hero.position.y);
        let enemy_pos = new Vec2(this.node.position.x,this.node.position.y);

        //计算人物和敌人之间偏移量
        Vec2.subtract(subPos,hero_pos,enemy_pos);

        //计算英雄和敌人之前的长度
        let length = subPos.length();

        //向英雄移动
        if(length > this.attackRange){
            let normal_pos = enemy_pos.add(subPos.normalize());
            this.node.position = new Vec3(normal_pos.x,normal_pos.y,0)
        }

        //移动方向
        let pic = this.node.getChildByName("pic");
        pic.scale = new Vec3(subPos.x > 0 ? -1 : 1,1,1);
    }

    //生成肉
    enemyDieCreateTool(){
        find("Canvas").getComponent(gameComponent).dieCreateTool(this.node);
    }

    checkAttackHeroRange(range){
        return range <= this.attackRange ? true : false;
    }

    //更新敌人的血量
    updateEnemyBlood(value){
        if(this.curBlood <= 0) return;
        this.curBlood -= value;
        let blood_progress = this.node.getChildByName("blood");
        blood_progress.getComponent(ProgressBar).progress = this.curBlood / this.blood;
        if(this.curBlood <= 0){
            console.log("==========敌人消除=========");
            this.node["unCheck"] = true;
            find("Canvas").getComponent(gameComponent).updateContentList();
            this.enemyDieCreateTool();
            let self = this;
            setTimeout(()=>{
                this.createMeat();
                self.node.destroy();
            },100)
        }
    }

    protected update(dt: number): void {

        this.updateAttackChiling();

        find("Canvas").getComponent(gameComponent).checkEnemyAttackHero(this.node);
    }
}


